#include <stdio.h>
#include <gtk/gtk.h>
#include <string.h>
#include <cairo.h>
//游戏常量（网格数）
#define GRID_WIDTH 20
#define GRID_HEIGHT 20


// 绘制网格的回调函数，用于 GtkDrawingArea 的 draw 信号
static gboolean draw_grid_callback(GtkWidget *widget, cairo_t *cr, gpointer user_data) {
    int width = gtk_widget_get_allocated_width(widget);
    int height = gtk_widget_get_allocated_height(widget);

    // 设置网格线颜色为灰色
    cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
    cairo_set_line_width(cr, 1);
    // 绘制水平网格线
    for (int y = 0; y <= height; y += GRID_HEIGHT) {
        cairo_move_to(cr, 0, y);
        cairo_line_to(cr, width, y);
        cairo_stroke(cr);
    }
    // 绘制垂直网格线
    for (int x = 0; x <= width; x += GRID_WIDTH) {
        cairo_move_to(cr, x, 0);
        cairo_line_to(cr, x, height);
        cairo_stroke(cr);
    }
    // 设置蛇的颜色为绿色
    cairo_set_source_rgb(cr, 0.1, 0.3, 0.1);
    cairo_set_line_width(cr, 0);  // 填充模式，无需线条宽度（如果要描边可调整）
    // 初始蛇的位置：左上角，长度为 2，方向水平向右
    // 蛇的身体由方块组成，这里简单用两个相邻的方块表示
    // 第一个方块（蛇头）位置：(0, 0) 对应的网格区域
    cairo_rectangle(cr,
                    0,  // 方块 x 坐标
                    0,  // 方块 y 坐标
                    GRID_WIDTH,  // 方块宽度
                    GRID_HEIGHT); // 方块高度
    cairo_fill(cr);  // 填充绘制蛇头

    // 第二个方块（蛇身）位置：(GRID_WIDTH, 0) ，水平向右
    cairo_rectangle(cr,
                    GRID_WIDTH,
                    0,
                    GRID_WIDTH,
                    GRID_HEIGHT);
    cairo_fill(cr);  // 填充绘制蛇身
    return FALSE;
}

//回调函数
static GtkWidget* create_game_page(int a){
    GtkWidget *game_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(game_window),"蛇，不弱！");
    gtk_window_set_default_size(GTK_WINDOW(game_window),GRID_WIDTH*a,GRID_HEIGHT*a);
    g_signal_connect(game_window,"destroy",NULL,NULL);//关闭游戏窗口
    // 创建绘图区域
    GtkWidget *drawing_area = gtk_drawing_area_new();
    gtk_container_add(GTK_CONTAINER(game_window), drawing_area);

    // 连接绘图信号回调，当需要绘制时调用 draw_grid_callback
    g_signal_connect(G_OBJECT(drawing_area), "draw", G_CALLBACK(draw_grid_callback), NULL);

    return game_window;
}

//回调函数：打开游戏
static void play_to_game(GtkWidget *button,gpointer data){
    int a = GPOINTER_TO_INT(data);
    GtkWidget *grid_window = create_game_page(a);
    gtk_widget_show_all(GTK_WINDOW(grid_window));
}

//回调函数：跳转到排行榜
static void switch_to_ranking(GtkWidget *button,gpointer data){
    GtkStack *stack = GTK_STACK(data);
    gtk_stack_set_visible_child_name(stack,"read_rank");
}

//回调函数：跳转到选关界面
static void switch_to_select2(GtkWidget *button,gpointer data){
    GtkStack *stack = GTK_STACK(data);
    gtk_stack_set_visible_child_name(stack,"select_diff");
}
//回调函数：回到主页
static void switch_to_homepage(GtkWidget *button,gpointer data){
    GtkWidget *stack = GTK_STACK(data);
    gtk_stack_set_visible_child_name(stack,"homepage");
}


int main(int argc ,char *argv[]) {
    gtk_init(&argc,&argv);
    //创建窗口
    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window),"蛇，不弱！");
    gtk_window_set_default_size(GTK_WINDOW(window),500,400);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
    //创建垂直布局
    GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL,0);
    gtk_container_add(GTK_CONTAINER(window),vbox);



    //创建GtkStack用于管理多个界面
    GtkStack *stack = gtk_stack_new();
    //将stack添加到主布局
    gtk_box_pack_start(GTK_BOX(vbox),stack,TRUE,TRUE,0);
    //设置页面切换方式为左右平滑
    gtk_stack_set_transition_type(GTK_STACK(stack),GTK_STACK_TRANSITION_TYPE_SLIDE_LEFT_RIGHT);
    //设置动画时间
    gtk_stack_set_transition_duration(GTK_STACK(stack),300);
    //========创建主页界面========
    //创建上下排序的按钮
    //创建主界面容器，并设置初始间距25
    GtkWidget *homepage = gtk_box_new(GTK_ORIENTATION_VERTICAL,25);
    //设置边距
    gtk_container_set_border_width(GTK_CONTAINER(homepage),20);
    //将主界面添加到stack
    gtk_stack_add_titled(GTK_STACK(stack),homepage,"homepage","主页");
    //设置界面按钮边长常量
    const int BUTTON_WIDTH = 200;
    const int BUTTON_HEIGH = 50;
    //创建初始界面三个按钮
    //gtk_container_add(GTK_CONTAINER(window),homepage);
    GtkWidget *button_start = gtk_button_new_with_label("开始游戏");
    //设置顶部距离
    gtk_widget_set_margin_top(button_start, 90);
    gtk_widget_set_size_request(button_start,BUTTON_WIDTH,BUTTON_HEIGH);
    gtk_box_pack_start(GTK_BOX(homepage),button_start,FALSE,FALSE,0);
    gtk_widget_set_halign(button_start,GTK_ALIGN_CENTER);
    //为游戏界面单独添加一个window，在点击开始按钮后跳转到新的界面
    //最后一位设置参数，可以在不同难度情况下调整参数以改变地图大小，实现函数复用，详见create_game_page函数以及play_to_game函数
    g_signal_connect(button_start,"clicked",G_CALLBACK(play_to_game),(void *)(10));

    //创建主界面选择关卡按钮
    GtkWidget *button_select = gtk_button_new_with_label("选择关卡");
    gtk_widget_set_size_request(button_select,BUTTON_WIDTH,BUTTON_HEIGH);
    gtk_box_pack_start(GTK_BOX(homepage),button_select,FALSE,FALSE,0);
    gtk_widget_set_halign(button_select,GTK_ALIGN_CENTER);

    //创建主界面排行榜按钮
    GtkWidget *button_rank = gtk_button_new_with_label("排行榜");
    gtk_widget_set_size_request(button_rank,BUTTON_WIDTH,BUTTON_HEIGH);
    gtk_box_pack_start(GTK_BOX(homepage),button_rank,FALSE,FALSE,0);
    gtk_widget_set_halign(button_rank,GTK_ALIGN_CENTER);

    //======选关界面======
    GtkWidget *select_diff = gtk_box_new(GTK_ORIENTATION_VERTICAL,10);
    GtkWidget *button_back_homepage = gtk_button_new_with_label("返回主页");
    gtk_widget_set_size_request(button_back_homepage,90,40);
    gtk_box_pack_start(GTK_BOX(select_diff),button_back_homepage,FALSE,FALSE,0);
    gtk_widget_set_halign(button_back_homepage,GTK_ALIGN_CENTER);

    //选择关卡难度 创建按钮
    GtkWidget *button_simple = gtk_button_new_with_label("菜鸟");
    GtkWidget *button_commen = gtk_button_new_with_label("一般");
    GtkWidget *button_difficult = gtk_button_new_with_label("困难");
    //调整按钮大小
    gtk_widget_set_size_request(button_simple,BUTTON_WIDTH,BUTTON_HEIGH);
    gtk_widget_set_size_request(button_commen,BUTTON_WIDTH,BUTTON_HEIGH);
    gtk_widget_set_size_request(button_difficult,BUTTON_WIDTH,BUTTON_HEIGH);
    //添加按钮
    gtk_box_pack_start(GTK_BOX(select_diff),button_simple,FALSE,FALSE,5);
    gtk_widget_set_margin_top(button_simple, 30);
    gtk_box_pack_start(GTK_BOX(select_diff),button_commen,FALSE,FALSE,5);
    gtk_box_pack_start(GTK_BOX(select_diff),button_difficult,FALSE,FALSE,5);
    gtk_widget_set_halign(button_simple,GTK_ALIGN_CENTER);
    gtk_widget_set_halign(button_commen,GTK_ALIGN_CENTER);
    gtk_widget_set_halign(button_difficult,GTK_ALIGN_CENTER);
    //创建关系
    g_signal_connect(button_simple,"clicked",G_CALLBACK(play_to_game),GINT_TO_POINTER(10));
    g_signal_connect(button_commen,"clicked",G_CALLBACK(play_to_game),GINT_TO_POINTER(20));
    g_signal_connect(button_difficult,"clicked",G_CALLBACK(play_to_game),GINT_TO_POINTER(30));

    //修改按钮距离顶部的距离
    gtk_widget_set_margin_top(button_back_homepage,40);
    // 将新界面添加到 stack
    gtk_stack_add_titled(GTK_STACK(stack), select_diff, "select_diff", "新界面");
    // === 连接按钮回调函数 ===
    // 主界面按钮：点击后切换到新界面
    g_signal_connect(button_select, "clicked", G_CALLBACK(switch_to_select2), stack);

    // 新界面按钮：点击后返回主界面
    g_signal_connect(button_back_homepage, "clicked", G_CALLBACK(switch_to_homepage), stack);

    //======排行榜界面======
    GtkWidget *read_rank = gtk_box_new(GTK_ORIENTATION_VERTICAL,10);
    GtkWidget *button_back_homepage1 = gtk_button_new_with_label("返回主页");
    gtk_widget_set_size_request(button_back_homepage1,90,40);
    gtk_box_pack_start(GTK_BOX(read_rank),button_back_homepage1,FALSE,FALSE,0);
    gtk_widget_set_halign(button_back_homepage1,GTK_ALIGN_CENTER);

    // 将新界面添加到 stack
    gtk_stack_add_titled(GTK_STACK(stack), read_rank, "read_rank", "新界面");
    // 主界面按钮：点击后切换到新界面
    g_signal_connect(button_rank, "clicked", G_CALLBACK(switch_to_ranking), stack);

    // 新界面按钮：点击后返回主界面
    g_signal_connect(button_back_homepage1, "clicked", G_CALLBACK(switch_to_homepage), stack);

    gtk_widget_show_all(GTK_WINDOW(window));
    gtk_main();
    return 0;
}
